const usernameInput = document.getElementById('username')
const emailInput = document.getElementById('email')


usernameInput.addEventListener('keyup', (e) => {
    const username = e.target.value;
    console.log(username)
    console.log('dianji')
    // reset
    e.target.classList.remove('is-invalid');
    e.target.nextElementSibling.innerText = '';

    fetch('/authentication/validate-username', {
        method: 'POST',
        body: JSON.stringify({ username })
    }).then((res) => res.json()).then((data) => {
        if (data.status == 'error') {
            e.target.classList.add('is-invalid');
            e.target.nextElementSibling.innerText = data.msg;
        }
    }).catch((err) => {
        console.log(err)
    })

})


emailInput.addEventListener('keyup', (e) => {
    const email = e.target.value;
    
    // reset
    e.target.classList.remove('is-invalid');
    e.target.nextElementSibling.innerText = '';

    fetch('/authentication/validate-email', {
        method: 'POST',
        body: JSON.stringify({ email })
    }).then((res) => res.json()).then((data) => {
        if (data.status == 'error') {
            e.target.classList.add('is-invalid');
            e.target.nextElementSibling.innerText = data.msg;
        }
    })

})